<template>
  <div>
    <v-dialog :title="isEdit?'结算方式设置编辑':'结算方式设置新增'" :show.sync="dialogVisible" width="1200px">
      <div class="card-box">
        <el-form ref="editForm" :model="form" class="form-wrap" size="small" :rules="rules">
          <el-form-item label="结算方式类型" prop="settlementType">
            <el-select v-model="form.settlementType" placeholder="请选择">
              <el-option v-for="item in settlement_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算方式名称" prop="settlementName">
            <el-input v-model="form.settlementName" maxlength="50" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="结算方式编码" prop="settlementCode">
            <el-input v-model="form.settlementCode" maxlength="50" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="备注" class="full" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="foot">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button v-throttle=" [commit]">提 交</el-button>
      </span>
    </v-dialog>
  </div>
</template>

<script>
import engineerList from '@/components/engineer-list'
import { addObj, putObj } from '../../../api/finance-manage-settlement'
import { mapState } from 'vuex'
import BaseLoad from '@/assets/vue-mixin/BaseLoad'

export default {
  mixins: [BaseLoad],
  components: { engineerList },
  computed: {
    ...mapState({
      settlement_type: state => {
        let data = state.dict.find(item => item.type === "settlement_type") || {}
        let list = data.children || []
        return data.children || []
      },
    })
  },
  data () {
    return {
      fileSize: 50,
      formData: {},
      input: '',
      value: '',
      area: [],
      form: {
        settlementType: '',
        settlementName: '',
        settlementCode: '',
        remarks: '',
      },
      dialogVisible: false,
      isEdit: false,
      rules: {
        settlementType: [{ required: true, message: '请选择结算方式类型', trigger: 'blur' }],
        settlementName: [{ required: true, message: '请输入结算方式名称', trigger: 'blur' }],
        settlementCode: [{ required: true, message: '请输入结算方式编码', trigger: 'change' }],
      }
    }
  },
  methods: {
    show (item) {
      this.dialogVisible = true
      setTimeout(() => {
        this.isEdit = false
        this.$refs.editForm.resetFields()
        delete this.form.propertyId
        if (item) {
          this.isEdit = true
          this.form = item
          Object.keys(this.form).forEach(key => {
            this.form[key] = item[key]
          })
          this.form.propertyId = item.propertyId
        }
      }, 200)
    },
    commit () {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            putObj(this.form).then(resp => {
              if (resp.data.data) {
                this.suc('提交成功')
                this.dialogVisible = false
                this.$emit('resetList')
              } else {
                this.err('提交失败')
              }
            })
          } else {
            addObj(this.form).then(resp => {
              if (resp.data.data) {
                this.suc('添加成功')
                this.dialogVisible = false
                this.$emit('resetList')
              } else {
                this.err('添加失败')
              }
            })
          }
        }
      })
    },
    beforeUpload () {
      this.loadAction('请稍等...')
    }
  }
}
</script>

<style lang='scss' scoped>
@include data-title();
@include info-list(2);
@include formWrap(3);
</style>
